#include "devnodelist.h"
#include <QDebug>

devNodeList::devNodeList(QObject *parent) : QObject(parent)
{

}

int devNodeList::getNode(int i, devNode &destnode)
{
    if (i < 0 || i >= MAX_CONN_DEV)
        return -1;
//    destnode = node.value(i);
    return 0;
}

devNode *devNodeList::findnode(QString devname, int &index)
{
    if (devname.isEmpty() || !index)
        return (NULL);

    for (int i = 0; i < MAX_CONN_DEV; i ++) {
        if (node.at(i).state == NST_EMPTY)
            continue;
        if (devname.compare(node.at(i).devName)) {
            index = i;
            return &node.value(i);
        }
    }

    return NULL;
}

int	devNodeList::addNode(devNode &new_node)
{
//    int i = 0 ,bfind = 0;

//    /* first to check if there is an offline data */
//    for (i = 0 ; i < MAX_CONN_DEV ; i ++) {
//        /* if in offline-mode */
//        if (NST_OFFLN == node [i].state) {
//            if (0 == strcmp ((const char *) node [i].devname,new_node.devname)) {
//                /* if matched ,which means we have a same devname in offline state */

//                /* @1 delete the whole node */
//                ASSERT(0);
//                return (0);
//            }
//        }
//    }

//    /* not found in offline list ,then we add it */
//    for (i = 0 ; i < MAX_CONN_DEV ; i++) {
//        /* if in empty state */
//        if (NST_EMPTY == node[i].state) {
//            node [i] = new_node;
//            return (0);
//        }
//    }
    if(node.count() <= MAX_CONN_DEV){
        node.insert(node.count(), new_node);
        return (0);
    }
    /* full slot */
    return (-1);
}

int devNodeList::printNodeList()
{
    qDebug()<<"==========================";
    for(int i = 0; i < node.count(); i++){
        qDebug()<<node.at(i).devName;
    }
    qDebug()<<"==========================";
    return node.count();
}
